Page({
  /**
   * 页面的初始数据
   */
  data: {
    title: '',
    content: '',
    id: '',
    status: '',
    explanation: '',
    author: '',
    dynasty: '',
    analyzeStatus:''
  },

  // 收藏
  collect: function (e) {
    switch (this.data.status) {
      case 1:
        this.setData({
          status: 0
        })
        break
      case 0:
        this.setData({
          status: 1
        })
    }

    let tuser = wx.getStorageSync('user')
    console.log("进入collecr函数中：");
    console.log(tuser.id);
    console.log(this.data.id);
    let data = {
      Userid: tuser.id,
      Poemid: this.data.id,
    }
    wx.request({
      url: 'http://localhost:8080/collect/addcollect/' + tuser.id,
      method: 'POST',
      data: this.data.id,
      header: {
        // "Content-Type": "application/x-www-form-urlencoded"
        'content-type': 'application/json' // 默认值
      },
    })
  },

  // 通过页面传参过来的标题获取诗词
  getContent: function (e) {
    var that = this;
    wx.request({
      url: 'http://localhost:8080/poem/select',
      method: 'POST',
      data: this.data.title,
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        var message = res.data;
        // console.log(res.data);
        if (message == null) {
          var toastText = '数据获取失败';
          wx.showToast({
            title: toastText,
            icon: '',
            duration: 2000
          });
        } else {
          that.setData({
            message: res.data,
            content: res.data[0].paragraphs,
            id: res.data[0].id,
            author: res.data[0].author,
            dynasty: res.data[0].dynasty,
            analyze: res.data[0].analyze
          })
          that.judge();
        }
      }
    })
  },

  // 判断该内容用户是否收藏，0=未收藏，1=已经收藏
  judge: function (e) {
    var that = this;
    let tuser = wx.getStorageSync('user')
    console.log("进入judge函数：");
    console.log(tuser.id);
    console.log(this.data.id);
    let data = {
      Userid: tuser.id,
      Poemid: this.data.id,
    }
    wx.request({
      url: 'http://localhost:8080/collect/judge/' + tuser.id,
      method: 'POST',
      data: this.data.id,
      header: {
        // "Content-Type": "application/x-www-form-urlencoded"
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        var status = res.data;
        console.log(status);
        if (status == null) {
          var toastText = '数据获取失败';
          wx.showToast({
            title: toastText,
            icon: '',
            duration: 2000
          });
        } else {
          that.setData({
            status: res.data,
          })
        }
      }
    })

    if (this.data.analyze == null) {
      that.setData({
        analyzeStatus: 0,
      })
    } else {
      that.setData({
        analyzeStatus: 1,
      })
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      title: options.id,
    })
    this.getContent();
  },
})